﻿using MainService.UISERVER;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace MainService
{
    public static class UIServer
    {
        public static string GetServerUrl(string url)
        {
            int index = url.IndexOf('/', 7);
            url = url.Substring(index);
            ServerAddress sa = MyServer.ServerConfig.UISERVER;
            return "http://" + sa.ServerIP + ":" + sa.ServerPort + url;
        }

        public static string Login(string username, string password,ref string userid) {
             if(username=="lainiao"&&password=="lainiao")
             {
                userid="11111111";
                return "success";
             }
             return "error";
        }

        public static MyResult SaveAppSystem(Systems systems)
        {
            MyResult result = null;
            XmlSerializer xs = new XmlSerializer(typeof(Systems));
            string xml = string.Empty;
            using (Stream sm = new MemoryStream()) 
            {
                xs.Serialize(sm, systems);
                sm.Position = 0;
                StreamReader sr = new StreamReader(sm);
                xml = sr.ReadToEnd(); 
            }
            UISERVER_SYSTEM.UISystemService uiss = new UISERVER_SYSTEM.UISystemService();
            uiss.Url = GetServerUrl(uiss.Url);
            string mess = uiss.saveAppSystem(xml);
            xs = new XmlSerializer(typeof(MyResult));
            using(Stream sm=new MemoryStream(Encoding.UTF8.GetBytes(mess)))
            {
                result = (MyResult)xs.Deserialize(sm);
            }
            return result;
        }
    }
}
